
const Mock=require('mockjs');   
let db=Mock.mock({
    'data|9-10':[{
        id:'@id',
        name:'@name',
        'age|18-32':1,
        email:'@EMAIL',
        phone:/\d{11}/,
        website:Mock.Random.url('http')
    }],
    'page|10-100':100,
    'limit':10,
    errorCode:0
});     

module.exports={
    'GET /list/lists': (req, res) => { 
        console.log(req.query);
        db.limit=req.query.limit;
        db.total=db.page*db.limit-3;
        //res.status(200).json({a:'star'});
        res.status(200).json(db)
    },
    'POST /list/lists':(req,res)=>{
        console.log(req.body);
        let user=req.body;
        user.id=Mock.mock('@id');
        db.data.push(user);
        res.status(200).json(user);
    },
    'DELETE /list/lists/:id':(req,res)=>{
        //console.log(req.params);
        db.data=db.data.filter((i)=>{
            return i.id!=req.params.id;
        })
        res.status(200).json(db);
    },
    'POST /list/lists/:id':(req,res)=>{
        console.log(req.params,req.body);
        db.data=db.data.map((i)=>{
            if(i.id==req.params.id){
                i=req.body;
                i.id=req.params.id;
            }
            
            return i;
        })
        res.status(200).json(db);
    }
}